package com.zzmcc.jms.controller;

import com.jfinal.aop.Before;
import com.jfinal.ext.render.excel.JxlsRender;
import com.jfinal.kit.PathKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.zzmcc.base.BaseController;
import com.zzmcc.common.AuthorizeInterceptor;
import com.zzmcc.jms.model.Project;
import util.ChristStringUtil;
import util.ModelUtils;
import util.StringUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zzmcc.jms.common.ModelConfigWms.TABLE_Project;


public class ProjectOrderController extends BaseController {
    private static final Logger log =  Logger.getLogger(ProjectOrderController.class);

	public void index() {

        StringBuffer whee = new StringBuffer();
        List<Object> param = new ArrayList<Object>();
        String name = getPara("demand.name");
		String demandMemo = getPara("demand.demandMemo");
		String code = getPara("demand.code");
		String isSelect = getPara("demand.isSelect");
		String isSelectConfirm = getPara("demand.isSelectConfirm");
		String isReach = getPara("demand.isReach");
		String isCm = getPara("demand.isCm");
		String isCt = getPara("demand.isCt");
		String isCu = getPara("demand.isCu");
		String isCmOrderConfirm = getPara("demand.isCmOrderConfirm");
		String isCtOrderConfirm = getPara("demand.isCtOrderConfirm");
		String isCuOrderConfirm = getPara("demand.isCuOrderConfirm");

		String cmCreateDate = getPara("demand.cmCreateDate");
		String ctCreateDate = getPara("demand.ctCreateDate");
		String cuCreateDate = getPara("demand.cuCreateDate");

		String demandOwner = getPara("demand.demandOwner");
        String beginDate = getPara("beginDate");
		String endDate = getPara("endDate");
		String homeCounty = getPara("demand.homeCounty");

        if(ChristStringUtil.isNotEmpty(name)){
            whee.append(" and name like ?");
            param.add("%" + name + "%");
        }

		if(ChristStringUtil.isNotEmpty(demandMemo)){
			whee.append(" and demandMemo like ?");
			param.add("%" + demandMemo + "%");
		}

		if(ChristStringUtil.isNotEmpty(code)){
			whee.append(" and code like ?");
			param.add("%" + code + "%");
		}
		if(ChristStringUtil.isNotEmpty(demandOwner)){
			whee.append(" and demandOwner = ?");
			param.add(demandOwner);
		}

		if(ChristStringUtil.isNotEmpty(homeCounty)){
			whee.append(" and homeCounty = ?");
			param.add(homeCounty);
		}
		if(ChristStringUtil.isNotEmpty(isSelect)){
			whee.append(" and isSelect = ?");
			param.add(isSelect);
		}
		if(ChristStringUtil.isNotEmpty(isReach)){
			whee.append(" and coalesce(isReach,'否') = ?");
			param.add(isReach);
		}
		if(ChristStringUtil.isNotEmpty(isCm)){
			whee.append(" and coalesce(isCm,'否') = ?");
			param.add(isCm);
		}
		if(ChristStringUtil.isNotEmpty(isCt)){
			whee.append(" and coalesce(isCt,'否') = ?");
			param.add(isCt);
		}
		if(ChristStringUtil.isNotEmpty(isCu)){
			whee.append(" and coalesce(isCu,'否') = ?");
			param.add(isCu);
		}
		if(ChristStringUtil.isNotEmpty(isCmOrderConfirm)){
			whee.append(" and coalesce(isCmOrderConfirm,'否') = ?");
			param.add(isCmOrderConfirm);
		}
		if(ChristStringUtil.isNotEmpty(isCtOrderConfirm)){
			whee.append(" and coalesce(isCtOrderConfirm,'否') = ?");
			param.add(isCtOrderConfirm);
		}
		if(ChristStringUtil.isNotEmpty(isCuOrderConfirm)){
			whee.append(" and coalesce(isCuOrderConfirm,'否') = ?");
			param.add(isCuOrderConfirm);
		}

		if(ChristStringUtil.isNotEmpty(isSelectConfirm)){
			whee.append(" and coalesce(isSelectConfirm,'否') = ?");
			param.add(isSelectConfirm);
		}
		if(ChristStringUtil.isNotEmpty(cmCreateDate)){
			whee.append(" and TO_DAYS(cmCreateDate) = TO_DAYS(?)");
			param.add( cmCreateDate );
		}
		if(ChristStringUtil.isNotEmpty(ctCreateDate)){
			whee.append(" and TO_DAYS(ctCreateDate) = TO_DAYS(?)");
			param.add( ctCreateDate );
		}
		if(ChristStringUtil.isNotEmpty(cuCreateDate)){
			whee.append(" and TO_DAYS(cuCreateDate) = TO_DAYS(?)");
			param.add( cuCreateDate );
		}

		if(ChristStringUtil.isNotEmpty(beginDate)){
			whee.append(" and TO_DAYS(createDate) >= TO_DAYS(?)");
			param.add( beginDate );
		}
		if(ChristStringUtil.isNotEmpty(endDate)){
			whee.append(" and TO_DAYS(createDate) <= TO_DAYS(?)");
			param.add(endDate);
		}
		Page<Project> pager = Project.dao.paginate(
				getParaToInt("pageNum", 1),
				getParaToInt("numPerPage", 20),
                "select a.*,b.reachDate ",
                " from  jms_project a left join jms_project_build b " +
						"on a.pid=b.pid where isSelectConfirm='是' "+ whee.toString()+"order by selectConfirmDate desc",
                param.toArray());

		setAttr("pager",pager);
		setAttr("name",name);
		setAttr("demandMemo",demandMemo);
		setAttr("code",code);
		setAttr("demandOwner",demandOwner);
		setAttr("isSelect",isSelect);
		setAttr("isSelectConfirm",isSelectConfirm);
		setAttr("isReach",isReach);
		setAttr("isCm",isCm);
		setAttr("isCt",isCt);
		setAttr("isCu",isCu);

		setAttr("isCmOrderConfirm",isCmOrderConfirm);
		setAttr("isCtOrderConfirm",isCtOrderConfirm);
		setAttr("isCuOrderConfirm",isCuOrderConfirm);

		setAttr("cmCreateDate",cmCreateDate);
		setAttr("ctCreateDate",ctCreateDate);
		setAttr("cuCreateDate",cuCreateDate);


		setAttr("beginDate",beginDate);
		setAttr("endDate",endDate);
		setAttr("homeCounty",homeCounty);
		render("../project_order_list.html");

	}



	public void update() {
		Project s = getModel(Project.class, "project");
		ModelUtils.fillForUpdate(s);
		s.update();
		renderDWZSuccessJson("修改成功！");
	}

	@Before(AuthorizeInterceptor.class)
	public void editCm() {

		Project project = Project.dao.findById(getPara("id"));
		setAttr("project",project);
		String isCm=project.get("isCm");
		if (StringUtil.isNotEmpty(isCm)&&isCm.equals("是")){
			render("../demand_input_cm.html");
		} else {
			render("../project_operate_tip.html");
		}
	}

	@Before(AuthorizeInterceptor.class)
	public void editCt() {
		Project project = Project.dao.findById(getPara("id"));
		setAttr("project", project);
		String isCt=project.get("isCt");
		if (StringUtil.isNotEmpty(isCt)&&isCt.equals("是")){
			render("../demand_input_ct.html");
		} else {
			render("../project_operate_tip.html");
		}
	}

	@Before(AuthorizeInterceptor.class)
	public void editCu() {
		Project project = Project.dao.findById(getPara("id"));
		setAttr("project", project);
		String isCu=project.get("isCu");
		if (StringUtil.isNotEmpty(isCu)&&isCu.equals("是")){
			render("../demand_input_cu.html");
		} else {
			render("../project_operate_tip.html");
		}
	}
	

	public void view() {
		setAttr("project",Project.dao.findById(getPara("id")));
		render("../demand_detail.html");
	}


	public void export() {

		//导出条件
		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();

		String isCm = getPara("demand.isCm");
		String isCt = getPara("demand.isCt");
		String isCu = getPara("demand.isCu");

		if(ChristStringUtil.isNotEmpty(isCm)){
			whee.append(" and isCm = ?");
			param.add(isCm);
		}
		if(ChristStringUtil.isNotEmpty(isCt)){
			whee.append(" and isCt = ?");
			param.add(isCt);
		}
		if(ChristStringUtil.isNotEmpty(isCu)){
			whee.append(" and isCu = ?");
			param.add(isCu);
		}

		String sql = "select *  from " + TABLE_Project +" where 1=1 "+ whee.toString()+"order by modifyDate desc";
		List<Record> demandDetail = Db.find(sql, param.toArray());

		Map<String, Object> beans = new HashMap<String, Object>();
		beans.put("demandDetail", demandDetail);

		String templateFileName = PathKit.getRootClassPath() + "/tpl_project_demand.xls";
		String filename = "project_demand.xls";
		render(JxlsRender.me(templateFileName).filename(filename).beans(beans));


	}


	@Before(AuthorizeInterceptor.class)
	public void transCmorder() {
		//移动需求转订单
		String[] ids = getParaValues("ids");

		for(String id:ids) {
			Project m = Project.dao.findById(id);
			String isCmOrderConfirm = m.getStr("isCmOrderConfirm");
			if (StringUtil.isNotEmpty(isCmOrderConfirm) && isCmOrderConfirm.equals("否")){
				m.set("isCmOrderConfirm", "是");
			}
			ModelUtils.fillForUpdate(m);
			m.update();
		}
		renderDWZSuccessJson("移动需求批量转订单成功!");

	}

	@Before(AuthorizeInterceptor.class)
	public void transCtorder() {
		//电信需求转订单
		String[] ids = getParaValues("ids");

		for(String id:ids) {
			Project m = Project.dao.findById(id);
			String isCtOrderConfirm = m.getStr("isCtOrderConfirm");
			if (StringUtil.isNotEmpty(isCtOrderConfirm) && isCtOrderConfirm.equals("否")){
				m.set("isCtOrderConfirm", "是");
			}
			ModelUtils.fillForUpdate(m);
			m.update();
		}
		renderDWZSuccessJson("电信需求批量转订单成功!");

	}

	@Before(AuthorizeInterceptor.class)
	public void transCuorder() {
		//联通需求转项目
		String[] ids = getParaValues("ids");

		for(String id:ids) {
			Project m = Project.dao.findById(id);
			String isCuOrderConfirm = m.getStr("isCuOrderConfirm");
			if (StringUtil.isNotEmpty(isCuOrderConfirm) && isCuOrderConfirm.equals("否")){
				m.set("isCuOrderConfirm", "是");
			}
			ModelUtils.fillForUpdate(m);
			m.update();
		}
		renderDWZSuccessJson("联通需求批量转订单成功!");

	}

}
